package o;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.downloadengine.api.SplitTask;
import com.huawei.appgallery.foundation.application.pkgmanage.model.update.ApkUpgradeInfo;
import com.huawei.appgallery.foundation.card.base.bean.BaseDistCardBean;
import com.huawei.appmarket.service.predownload.thread.PreDownloadManagerThread;
import com.huawei.hms.framework.common.ContainerUtils;
import com.huawei.hms.framework.network.restclient.hianalytics.HianalyticsData;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.List;
import o.dkb.d;
import o.dqw.c;

public class edw {
    public static void ॱ(ApkUpgradeInfo apkUpgradeInfo, SessionDownloadTask sessionDownloadTask, String str, String str2, int i, String str3, d dVar) {
        if (!(sessionDownloadTask == null || (apkUpgradeInfo.ʿ() == sessionDownloadTask.ॱᐝ() && ˎ(sessionDownloadTask.ᐝ(), apkUpgradeInfo.ʽॱ())))) {
            Logger.ˋ("DataProcessUtils", "db data is out time,delete zhe temp file");
            dvt.ˏ().ˊ(sessionDownloadTask.ˊˊ());
            sessionDownloadTask = null;
        }
        if (sessionDownloadTask == null) {
            Logger.ˎ("DataProcessUtils", "composeDownloadTask");
            ˎ(apkUpgradeInfo, str, str2, i, str3, dVar);
            return;
        }
        Logger.ˎ("DataProcessUtils", "DownloadTask exist:" + sessionDownloadTask.ʾ());
        if (dVar != null) {
            dVar.ˋ(sessionDownloadTask);
        }
    }

    private static void ˎ(ApkUpgradeInfo apkUpgradeInfo, String str, String str2, int i, String str3, d dVar) {
        BaseDistCardBean ˊ = dqw.ˊ(apkUpgradeInfo.ᐝ(), apkUpgradeInfo.ॱʽ(), apkUpgradeInfo.ʿ() + "");
        final ApkUpgradeInfo apkUpgradeInfo2 = apkUpgradeInfo;
        final String str4 = str3;
        final d dVar2 = dVar;
        final String str5 = str;
        final String str6 = str2;
        final int i2 = i;
        new dqw().ˏ(ˊ, new c() {
            public void ˋ(SessionDownloadTask sessionDownloadTask) {
                if (sessionDownloadTask != null) {
                    if (apkUpgradeInfo2.ॱʽ() == 0) {
                        SplitTask splitTask = new SplitTask();
                        String str;
                        LinkedHashMap linkedHashMap;
                        if (apkUpgradeInfo2.ʼ() > 0) {
                            splitTask.ˏ(apkUpgradeInfo2.ॱॱ());
                            splitTask.ॱ(apkUpgradeInfo2.ʼ());
                            splitTask.ˎ(edw.ˏ(apkUpgradeInfo2.ˈ(), "subsource", str4));
                            splitTask.ˋ(edw.ˏ(apkUpgradeInfo2.ʽॱ(), "subsource", str4));
                            if (!edw.ˊ(apkUpgradeInfo2.ˈ())) {
                                str = "composeDownloadTask diffDownload|" + apkUpgradeInfo2.ʽॱ() + '|' + apkUpgradeInfo2.ʻॱ() + '|' + apkUpgradeInfo2.ˈ() + '|' + apkUpgradeInfo2.ʼ();
                                linkedHashMap = new LinkedHashMap();
                                linkedHashMap.put(HianalyticsData.ERROR_CODE, "001");
                                linkedHashMap.put("error_desc", str);
                                cdl.ˎ("055", linkedHashMap);
                                if (dVar2 != null) {
                                    dVar2.ˋ(null);
                                    return;
                                }
                                return;
                            }
                        }
                        splitTask.ˋ(edw.ˏ(apkUpgradeInfo2.ˈ(), "subsource", str4));
                        if (edw.ˊ(apkUpgradeInfo2.ˈ())) {
                            str = "composeDownloadTask fullDownload|" + apkUpgradeInfo2.ʽॱ() + '|' + apkUpgradeInfo2.ʻॱ() + '|' + apkUpgradeInfo2.ˈ() + '|' + apkUpgradeInfo2.ʼ();
                            linkedHashMap = new LinkedHashMap();
                            linkedHashMap.put(HianalyticsData.ERROR_CODE, "002");
                            linkedHashMap.put("error_desc", str);
                            cdl.ˎ("055", linkedHashMap);
                            if (dVar2 != null) {
                                dVar2.ˋ(null);
                                return;
                            }
                            return;
                        }
                        splitTask.ˊ(apkUpgradeInfo2.ʾ());
                        splitTask.ˊॱ(apkUpgradeInfo2.ᐝ());
                        splitTask.ˋ(apkUpgradeInfo2.ʻॱ());
                        sessionDownloadTask.ˏ(splitTask);
                    }
                    sessionDownloadTask.ᐝ(str5);
                    sessionDownloadTask.ʼ(apkUpgradeInfo2.ᐝ());
                    sessionDownloadTask.ॱˊ(apkUpgradeInfo2.ॱʽ());
                    sessionDownloadTask.ˊ(edw.ˏ(apkUpgradeInfo2.ʽॱ(), "subsource", str4));
                    sessionDownloadTask.ॱॱ(apkUpgradeInfo2.ˊ());
                    sessionDownloadTask.ॱ(apkUpgradeInfo2.ॱ());
                    sessionDownloadTask.ʽ(apkUpgradeInfo2.ॱᐝ());
                    sessionDownloadTask.ʻ(apkUpgradeInfo2.ʿ());
                    sessionDownloadTask.ॱॱ(0);
                    sessionDownloadTask.ˏ(apkUpgradeInfo2.ˊᐝ());
                    sessionDownloadTask.ॱ(apkUpgradeInfo2.ˏˎ());
                    List<SplitTask> ᐝˊ = sessionDownloadTask.ᐝˊ();
                    if (!CollectCheck.isEmpty((List) ᐝˊ)) {
                        int i = 0;
                        for (SplitTask ʽ : ᐝˊ) {
                            ʽ.ʽ(str6 + apkUpgradeInfo2.ᐝ() + apkUpgradeInfo2.ॱ() + "_" + sessionDownloadTask.ʾ() + i);
                            i++;
                        }
                        sessionDownloadTask.ˋॱ(i2);
                    }
                }
                if (dVar2 != null) {
                    dVar2.ˋ(sessionDownloadTask);
                }
            }

            public void ॱ() {
                if (dVar2 != null) {
                    dVar2.ˋ(null);
                }
            }
        });
    }

    public static boolean ˊ(String str) {
        try {
            if (new URL(str).getPath().endsWith("apk")) {
                return false;
            }
            return true;
        } catch (Throwable e) {
            Logger.ˋ("DataProcessUtils", "url is invalid", e);
            return false;
        }
    }

    private static boolean ˎ(String str, String str2) {
        try {
            if (new URL(str).getPath().equals(new URL(str2).getPath())) {
                return true;
            }
        } catch (Throwable e) {
            Logger.ˋ("DataProcessUtils", "url is invalid", e);
        }
        return false;
    }

    public static void ˏ(ApkUpgradeInfo apkUpgradeInfo, int i) {
        if (apkUpgradeInfo != null && apkUpgradeInfo.ˏॱ() == 0) {
            Context ˋ = ContentManager.getInstant().getContext();
            int i2 = 8;
            if (!edl.ˏ().ˊˋ()) {
                i2 = 40;
            }
            if (!edl.ˏ().ˊᐝ()) {
                i2 |= 64;
            }
            if (edm.ˏ(ˋ).ˎ().contains(apkUpgradeInfo.ᐝ())) {
                i2 |= DnsConfig.MAX_CACHE_ENTRIES;
            }
            ˋ(ˋ, apkUpgradeInfo, i, i2);
        }
    }

    private static boolean ॱ(Context context, String str, int i) {
        if (epw.ॱ(context, str)) {
            Logger.ˎ("DataProcessUtils", "app can not be installed when app is using");
            ((edx) cdb.ॱ(edx.class)).ˊ(str);
            return false;
        }
        if ((i & 32) != 0) {
            String ˊ = euv.ˊ();
            if (!TextUtils.isEmpty(ˊ)) {
                Logger.ˎ("DataProcessUtils", "app can not be installed when game app(" + ˊ + ") is running, install app:" + str);
                ((edx) cdb.ॱ(edx.class)).ˋ(str, ˊ);
                return false;
            }
        }
        if ((i & DnsConfig.MAX_CACHE_ENTRIES) != 0 && euv.ॱ(context, str)) {
            Logger.ˎ("DataProcessUtils", "navigation app is using: " + str);
            ((edx) cdb.ॱ(edx.class)).ˏ("052", str);
            return false;
        } else if ((i & 64) == 0 || !euv.ˎ(context, str)) {
            return true;
        } else {
            Logger.ˎ("DataProcessUtils", "music is playing: " + str);
            ((edx) cdb.ॱ(edx.class)).ˏ("056", str);
            return false;
        }
    }

    private static void ˋ(Context context, ApkUpgradeInfo apkUpgradeInfo, int i, int i2) {
        if (((ceq) cdb.ॱ(ceq.class)).ˊ(context, apkUpgradeInfo.ᐝ()) && ॱ(context, apkUpgradeInfo.ᐝ(), i2)) {
            ॱ(context, apkUpgradeInfo, i, i2);
        } else {
            Logger.ˎ("DataProcessUtils", "The device didn't have enough storage space to install the app, or for some other reasons");
        }
    }

    private static void ॱ(Context context, ApkUpgradeInfo apkUpgradeInfo, int i, int i2) {
        int ˏ = i2 | etn.ˏ(3);
        coi o_edz = new edz(apkUpgradeInfo, i);
        fhz ˋ = fho.ˏ().ˋ("PackageManager");
        if (ˋ != null) {
            cnw o_cnw = (cnw) ˋ.ॱ(cnw.class);
            if (o_cnw != null) {
                cof ˎ = new cof.d().ˋ(apkUpgradeInfo.ᐝ()).ˏ(apkUpgradeInfo.ʿ()).ˊ(ˏ).ˊ(coc.UNCONCERN).ˏ(o_edz).ˎ();
                eeb o_eeb = (eeb) PreDownloadManagerThread.ˊ.get(Integer.valueOf(i));
                if (o_eeb != null) {
                    o_eeb.ˏ.incrementAndGet();
                }
                o_cnw.ˊ(context, ˎ);
                return;
            }
            Logger.e("DataProcessUtils", "can not found IPackageInstaller Api");
            return;
        }
        Logger.e("DataProcessUtils", "can not found PackageManager module");
    }

    public static SessionDownloadTask ˎ(List<SessionDownloadTask> list, String str) {
        int size = list.size();
        for (int i = 0; i < size; i++) {
            SessionDownloadTask sessionDownloadTask = (SessionDownloadTask) list.get(i);
            if (sessionDownloadTask != null && sessionDownloadTask.ˊˊ().equals(str)) {
                return sessionDownloadTask;
            }
        }
        return null;
    }

    public static String ˏ(String str, String str2, String str3) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        Uri parse = Uri.parse(str);
        if (!TextUtils.isEmpty(ceb.ˏ(parse, str2))) {
            return str;
        }
        String str4 = str2 + ContainerUtils.KEY_VALUE_DELIMITER + str3;
        String query = parse.getQuery();
        if (query != null) {
            query = query + ContainerUtils.FIELD_DELIMITER;
        }
        try {
            return new URI(parse.getScheme(), parse.getAuthority(), parse.getPath(), query + str4, parse.getFragment()).toString();
        } catch (URISyntaxException e) {
            Logger.e("DataProcessUtils", "error uri");
            return str;
        }
    }
}
